package com.xh.filepicker.sdk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xh.filepicker.sdk.R;
import com.xh.filepicker.sdk.models.DirectoryModel;

import java.util.ArrayList;


/**
 * Created by zhuangshaobo on 2021/6/15.
 */
public class DirectoryStackAdapter extends RecyclerView.Adapter<DirectoryStackAdapter.ViewHolder> {

    private Context context;
    private ArrayList<DirectoryModel> directoryList;
    private onDirectoryStackListener onDirectoryStackListener;

    public interface onDirectoryStackListener{
        void onDirClicked(DirectoryModel model);
    }

    public DirectoryStackAdapter(Context context, ArrayList<DirectoryModel> directoryList, onDirectoryStackListener listener) {
        this.context = context;
        this.directoryList = directoryList;
        this.onDirectoryStackListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fp_file_picker_item_directory_stack, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.tv_dir_name.setText(directoryList.get(position).getName());
        if(position == getItemCount()-1){
            holder.tv_dir_name.setTextColor(context.getResources().getColor(R.color.fp_text_dark));
        }else{
            holder.tv_dir_name.setTextColor(context.getResources().getColor(R.color.fp_text_light));
        }
        if (position > 0 && position == getItemCount()-1) {
            holder.iv_ic_arrow.setVisibility(View.GONE);
        } else {
            holder.iv_ic_arrow.setVisibility(View.VISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (directoryList.size() - 1 == position) {
                    return;
                }
                onDirectoryStackListener.onDirClicked(directoryList.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return directoryList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_dir_name;
        ImageView iv_ic_arrow;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_dir_name = itemView.findViewById(R.id.tv_dir_name);
            iv_ic_arrow = itemView.findViewById(R.id.ic_arrow);
        }
    }
}
